let express = require("express")()
const { response } = require("express");
let expressStatic = require("express-static");
const { request } = require("http");
let mysql = require("mysql")
const port = 1001

let sql = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '123456',
    database : 'students',
    timezone : '08:00'
  });
  
sql.connect();
  
express.get("/denglu",(request,response)=>{
    sql.query(`SELECT * FROM users WHERE username="${request.query.username}" AND password="${request.query.password}"`,(error,data)=>{
        if(error){
            console.log("error")
            response.send(error)
        }
        else{
            if(data.length === 0){
                response.send("error")
            }
            else{
                response.end("success")
            }
        }
    })
})
express.get("/zhuce",(request,response)=>{
    sql.query(`INSERT INTO users (username,password) VALUE ("${request.query.username}","${request.query.password}")`,(error,data)=>{
        if(error){
            console.log("error")
            response.send(error)
        }
        else{
            response.end("success")
        }
    })
})
express.get("/insert",(request,response)=>{
    sql.query(`SELECT * FROM student`,(error,data)=>{
        if(error){
            console.log("error")
            response.send(error)
        }
        else{
            response.send(data)
        }
    })
})

express.get("/delete",(request,response)=>{
    sql.query(`DELETE FROM student WHERE id="${request.query.id}"`,(error,data)=>{
        if(error){
            console.log(error)
            response.send("error")
        }
        else{
            response.send("success")
        }
    })
})

express.get("/addStudent",(request,response)=>{
    let p = request.query
    console.log(p.name)
    sql.query(`INSERT INTO student (name,age,sex,city,joinDate) VALUES ("${p.name}","${p.age}","${p.sex}","${p.city}","${p.joinDate}")`,(error,data)=>{
        if(error){
            console.log(error)
            response.send("error")
        }
        else{
            response.send("success")
        }
    })
})

express.get("/change",(request,response)=>{
    console.log(request.query.id)
    console.log(request.query.name)
    sql.query(`UPDATE student SET name="${request.query.name}",sex="${request.query.sex}",age="${request.query.age}",city="${request.query.city}",joinDate="${request.query.joinDate}" WHERE id="${request.query.id}"`,(error,data)=>{
        if(error){
            console.log(error)
            response.send("error")
        }
        else{
            response.send("success")
        }
    })
})

express.use(expressStatic(__dirname + "/static"))
console.log(`running of ${port}`)
express.listen(port)